#! /usr/bin/env python

# -*- coding: UTF-8 -*-
# ==============================================================================
# Filename : GoToFolder.py
# Purpose  : 
# Author   : Yohann Li (lyh001686@sina.com)
# Date     : 2020-08-10 16:38
# ==============================================================================
from PyQt5.QtCore import pyqtSignal, QStringListModel
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QGridLayout, QLabel, QCompleter, QComboBox, QShortcut
import sys
from conf.configuration import logger

name_list = ['Library', '']

# TODO 后续设置补全格式，模仿Finder; 还差completer


class GoToFolder(QWidget):
    position_signal = pyqtSignal(str)  # 点击OK后把指定路径发送给TreeView

    def __init__(self):
        super(GoToFolder, self).__init__()
        self.str_list = QStringListModel()
        self.comp = QCompleter()
        # self.set_comb()
        self._init_ui()

    def _init_ui(self):
        self.label = QLabel('Go To The Folder:')
        self.edit = QComboBox()
        self.edit.setEditable(True)
        self.edit.setCompleter(self.comp)
        self.btn_ok = QPushButton("OK")
        self.edit.lineEdit().returnPressed.connect(self.ok_clicked)
        self.btn_ok.clicked.connect(self.ok_clicked)
        self.btn_ok.setDefault(True)
        self.btn_Cancel = QPushButton("Calcel")
        self.btn_Cancel.clicked.connect(self.close)
        g_lay = QGridLayout()
        g_lay.addWidget(self.label, 0, 0, 1, 2)
        g_lay.addWidget(self.edit, 1, 0, 1, 4)
        g_lay.addWidget(self.btn_ok, 2, 2, 1, 1)
        g_lay.addWidget(self.btn_Cancel, 2, 3, 1, 1)
        self.setLayout(g_lay)
        QShortcut(QKeySequence.Close, self).activated.connect(self.close)  # 快捷键关闭窗口

    def ok_clicked(self):
        dst = self.edit.lineEdit().text()
        logger.debug(f"Go to {dst}")
        self.position_signal.emit(dst)
        self.close()

    def set_comb(self):
        self.str_list.setStringList(name_list)
        self.comp.setModel(self.str_list)
        self.comp.setModelSorting(QCompleter.CaseInsensitivelySortedModel)  # 不区分大小写
        self.comp.setCompletionMode(QCompleter.InlineCompletion)  # 弹出补全模式


if __name__ == '__main__':
    app = QApplication(sys.argv)
    gtf = GoToFolder()
    gtf.show()
    app.exec_()
